/*
 * @lc app=leetcode id=455 lang=cpp
 *
 * [455] Assign Cookies
 */

// @lc code=start
class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());
        int ans = 0;
        for (size_t i = 0, j = 0; i < s.size();) {
            if (j >= g.size()) {
                break;
            }
            if (s[i] >= g[j]) {
                ans++;
                j++;
                i++;
            } else {
                while (i < s.size() && s[i] < g[j] ) {
                    i++;
                }
                if (i >= s.size()) {
                    break;
                } else {
                    ans++;
                    j++;
                    i++;
                }
            }
        }
        return ans;
    }
};
// @lc code=end

